package com.yuan.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuan.reggie.common.CustomerException;
import com.yuan.reggie.mapper.CategoryMapper;
import com.yuan.reggie.mapper.DishMapper;
import com.yuan.reggie.mapper.EmployeeMapper;
import com.yuan.reggie.pojo.Category;
import com.yuan.reggie.pojo.Employee;
import com.yuan.reggie.service.CategoryService;
import com.yuan.reggie.service.DishService;
import com.yuan.reggie.service.EmployeeService;
import com.yuan.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yuanxingke
 * @date 2022/7/30 0:03
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        //首先判断是否存在

        QueryWrapper<Category> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("category_id",id);
        int count1 = dishService.count();

        if(count1>0){
            //关于菜品的定义
            throw new CustomerException("当前分类下关联了菜品，不能删除");
        }

        QueryWrapper<Category> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("category_id",id);
        int count2 = setmealService.count();

        if(count2>0){
            //关于套餐的定义
            throw new CustomerException("当前分类下关联了套餐，不能删除");
        }

        //如果都没问题，就执行删除
        this.remove(id);

    }
}
